package com.tsj.spzx.service.Impl;

import com.github.pagehelper.PageInfo;
import com.tsj.spzx.mapper.ProductDetailsMapper;
import com.tsj.spzx.mapper.ProductMapper;
import com.tsj.spzx.mapper.ProductSkuMapper;
import com.tsj.spzx.model.dto.product.ProductDto;
import com.tsj.spzx.model.entity.product.Product;
import com.tsj.spzx.model.entity.product.ProductDetails;
import com.tsj.spzx.model.entity.product.ProductSku;
import com.tsj.spzx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        List<Product> list = productMapper.findByPage(productDto);
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Transactional
    @Override
    public void save(Product product) {

        System.out.println("++++++++++++++ " + product + "++++++++++++++++++");

        // product数据存储在三张表中
        // 1、保存基本信息到product表
        productMapper.save(product);

        // 2、保存sku信息到product_sku表
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            // 获取到productSku对象，设置一些值
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(product.getId() + "-" + i);  // 商品编号，唯一就行
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            productSku.setProductId(product.getId());
            productSku.setStatus(0);  // 设置初始状态

            // 轮播图也要存在sku中
            // productSku.setThumbImg(product.getSliderUrls());

            // 保存sku
            productSkuMapper.save(productSku);
        }

        // 3、保存商品详情到product_details表（存放的详情图）
        // 先创建一个ProductDetails对象，设置一些值
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
    }

    @Override
    public Product getById(Long id) {
        // 1、根据id查询product表
        Product product = productMapper.getById(id);

        // 2、根据id查询product_sku表
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(id);
        product.setProductSkuList(productSkuList);

        // 3、根据id查询product_details表
        ProductDetails productDetails = productDetailsMapper.getById(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Transactional
    @Override
    public void updateById(Product product) {
        // 1、修改product表
        productMapper.update(product);

        // 2、修改product_sku表
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            productSkuMapper.update(productSku);
        }

        // 3、修改product_details表
        productDetailsMapper.updateById(product.getId(), product.getDetailsImageUrls());
    }

    @Transactional
    @Override
    public void deleteById(Long id) {
        // 1、删除product表
        productMapper.deleteById(id);

        // 2、删除product_sku表
        productSkuMapper.deleteByProductId(id);

        // 3、删除product_details表
        productDetailsMapper.deleteById(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus == 1){
            product.setAuditStatus(1);
            product.setAuditMessage("审核通过");
        }else{
            product.setAuditStatus(-1);
            product.setAuditMessage("审核不通过");
        }
        productMapper.update(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status == 1){
            product.setStatus(1);
        }else{
            product.setStatus(-1);
        }
        productMapper.update(product);
    }
}
